package com.chexiehui.jc.ui.activity;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aries.ui.widget.alert.UIAlertDialog;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.chexiehui.jc.Constants;
import com.chexiehui.jc.R;
import com.chexiehui.jc.base.BaseActivity;
import com.chexiehui.jc.utils.DataCleanManager;
import com.chexiehui.jc.utils.NoDoubleClickUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Author: SongJun
 * CreateTime: 2019/9/17 0017
 * Company: 贵州华创云科技有限公司
 * Description: 设置页面
 */
public class SettingActivity extends BaseActivity {

    @BindView(R.id.rl_account_manage_container)
    RelativeLayout rlAccountManageContainer;
    @BindView(R.id.tv_cache)
    TextView tvCache;
    @BindView(R.id.rl_clear_cache_container)
    RelativeLayout rlClearCacheContainer;
    @BindView(R.id.rl_wifi_net_container)
    RelativeLayout rlWifiNetContainer;
    @BindView(R.id.rl_push_set_container)
    RelativeLayout rlPushSetContainer;
    @BindView(R.id.tv_version)
    TextView tvVersion;
    @BindView(R.id.rl_version_container)
    RelativeLayout rlVersionContainer;
    @BindView(R.id.rl_login_out)
    RelativeLayout rlLoginOut;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;

    @Override
    protected void setTitleBar() {
        titleBar.setTitleMainText("设置");
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initView(Bundle var1) {
        mRefreshLayout.setEnableRefresh(false);
        mRefreshLayout.setEnableLoadMore(false);
        mRefreshLayout.setEnableOverScrollDrag(true);

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String totalStr = DataCleanManager.getTotalCacheSize(mContext);
                    Message msg = new Message();
                    msg.what = 1;
                    msg.obj = totalStr;
                    mHandle.sendMessage(msg);
                } catch (Exception e) {
                    e.printStackTrace();
                    Message msg = new Message();
                    msg.what = 1;
                    msg.obj = "0KB";
                    mHandle.sendMessage(msg);
                }
            }
        }).start();
        tvVersion.setText("V" + AppUtils.getAppVersionName());
    }

    @SuppressLint("HandlerLeak")
    private Handler mHandle = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                case 2:
                    String totalStr = (String) msg.obj;
                    tvCache.setText(totalStr);
                    break;
                case 3:
                case 4:
                    tvCache.setText("0KB");
                    break;
            }
        }
    };


    private void showLoginOutDialogTips() {
        new UIAlertDialog.DividerIOSBuilder(this)
                .setTitle("提示")
                .setTitleTextColorResource(R.color.colorAlertTitle)
                .setMessage("确定要退出登录？")
                .setMessageTextColorResource(R.color.colorAlertMessage)
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .setNegativeButtonTextColorResource(R.color.colorAlertButton)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        loginOut();
                    }
                })
                .setMinHeight(SizeUtils.dp2px(120))
                .create()
                .setDimAmount(0.6f)
                .show();
    }

    private void loginOut() {
        SPUtils.getInstance(Constants.LOGIN_SCOPE).clear(true);
        ActivityUtils.finishAllActivities();
        startActivity(LoginActivity.class);
    }

    @Override
    protected void onDestroy() {
        mHandle.removeCallbacksAndMessages(null);
        super.onDestroy();
    }


    @OnClick({R.id.rl_account_manage_container, R.id.rl_clear_cache_container, R.id.rl_wifi_net_container, R.id.rl_push_set_container, R.id.rl_version_container, R.id.rl_login_out})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_account_manage_container:
                startActivity(AccountSettingActivity.class);
                break;
            case R.id.rl_clear_cache_container:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            DataCleanManager.clearAllCache(mContext);
                            mHandle.sendEmptyMessage(3);
                        } catch (Exception e) {
                            e.printStackTrace();
                            mHandle.sendEmptyMessage(4);
                        }
                    }
                }).start();
                break;
            case R.id.rl_wifi_net_container:
                break;
            case R.id.rl_push_set_container:
                break;
            case R.id.rl_version_container:
                break;
            case R.id.rl_login_out:
                showLoginOutDialogTips();
                break;
        }
    }
}
